﻿@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@implements IDisposable
@inject IEventService _eventService
@inject IJSRuntime JS
@inject IUserService _userService


<div style=" position: fixed; z-index: 4; bottom: 7rem; right: 2rem;" class="@(_dataCacheService.IsApp?"d-none":"")">
    <div style=" display: flex; flex-direction: column;">
        @*<CnGalWebSite.Shared.MasaComponent.Shared.Cards.Entries.Brithdays.BrithdayButton Class="mb-4" />*@
        <CnGalWebSite.Shared.MasaComponent.Shared.Components.Catalogs.MasaCatalogButton @ref="masaCatalogButton" Class="mb-4" />
        @if (showEntryStyle)
        {
            <CnGalWebSite.Components.Buttons.MasaButton Fab Icon="@IconType.Style.ToIconString()" Tooltip="切换词条样式模板" OnClick="_eventService.OnSwitchStyle"  Class="mb-4"/>
        }

        <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaSpeedDial Icon="mdi-link" Direction="left" Text="社区交流" Class="mb-4">
            <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaImageToolTip Icon="mdi-wechat" Image="https://image.cngal.org/images/2022/06/02/76142d8159b0.jpg" />
            <CnGalWebSite.Components.Buttons.MasaButton XSmall Fab Icon="mdi-sina-weibo" Tooltip="微博" OnClick="@(()=>OnNav("https://weibo.com/cngalorg?sudaref=www.cngal.org"))" />
            <CnGalWebSite.Components.Buttons.MasaButton XSmall Fab Icon="mdi-qqchat" Tooltip="QQ" OnClick="@(()=>OnNav("https://jq.qq.com/?_wv=1027&k=mG6qNvyg"))" />

        </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaSpeedDial>

        <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaSpeedDial Icon="mdi-toolbox" Direction="left" Text="工具箱" Class="mb-4">
            <CnGalWebSite.Components.Buttons.MasaButton XSmall Fab Icon="mdi-archive" Tooltip="合并词条" OnClick="OnMergeEntries" />
            @if (ToolHelper.IsSSR)
            {
                <CnGalWebSite.Components.Buttons.MasaButton XSmall Fab Icon="mdi-script-text" Tooltip="转载文章" OnClick="OnRepostArticles" />
            }
            <CnGalWebSite.Components.Buttons.MasaButton XSmall Fab Icon="mdi-animation-play" Tooltip="转载视频" OnClick="OnRepostVideos" />
        </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaSpeedDial>

        <CnGalWebSite.Components.Buttons.MasaButton Fab Icon="mdi-transfer-up" Tooltip="回到顶部" OnClick="OnClickFly" />

    </div>

</div>
<CnGalWebSite.Shared.MasaComponent.Shared.Cards.Tools.MergeEntries.MainCard @ref="mergeEntriesCard" />
<CnGalWebSite.Shared.MasaComponent.Shared.Cards.Tools.RepostArticles.MainCard @ref="repostArticlesCard" />
<CnGalWebSite.Shared.MasaComponent.Shared.Cards.Tools.RepostVideos.MainCard @ref="repostVideosCard" />

 @code {
    private bool fab;
    private bool showEntryStyle;
    CnGalWebSite.Shared.MasaComponent.Shared.Cards.Tools.MergeEntries.MainCard mergeEntriesCard;
    CnGalWebSite.Shared.MasaComponent.Shared.Cards.Tools.RepostArticles.MainCard repostArticlesCard;
    CnGalWebSite.Shared.MasaComponent.Shared.Cards.Tools.RepostVideos.MainCard repostVideosCard;
    CnGalWebSite.Shared.MasaComponent.Shared.Components.Catalogs.MasaCatalogButton masaCatalogButton;

    public async Task OnClickFly()
    {
        await JS.InvokeVoidAsync("window.scrollTo", new { top = 0, behavior = "smooth" });
    }

    public async Task OnNav(string url)
    {
        await _eventService.OpenNewPage(url);
    }

    public string ComputeIcon()
    {
        return fab ? "mdi-close" : " mdi-account-circle";
    }

    public bool Login()
    {
        if (string.IsNullOrWhiteSpace(_dataCacheService.UserInfor?.Id))
        {
            _userService.Login();
            return false;
        }

        return true;
    }

    public async Task OnMergeEntries()
    {
        if (Login() == false)
        {
            return;
        }
        if (mergeEntriesCard != null)
        {
            await mergeEntriesCard.Refresh(true);
        }
    }

    public async Task OnRepostArticles()
    {
        if (Login() == false)
        {
            return;
        }
        if (repostArticlesCard != null)
        {
            await repostArticlesCard.Refresh(true);
        }
    }

    public async Task OnRepostVideos()
    {
        if (Login() == false)
        {
            return;
        }
        if (repostVideosCard != null)
        {
            await repostVideosCard.Refresh(true);
        }
    }

    public void DisplayArticleCatalogButton(ArticleViewModel model)
    {
        if (masaCatalogButton != null)
        {
            masaCatalogButton.Refresh(model.MainPage, $"/articles/index/{model.Id}");
        }

        StateHasChanged();
    }

    protected override void OnInitialized()
    {
        NavigationManager.LocationChanged -= LocationChanged;
        NavigationManager.LocationChanged += LocationChanged;
        LocationChanged(null,null);

    }

    public void LocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (NavigationManager.Uri.Contains("articles/index") == false)
        {

            if (masaCatalogButton != null)
            {
                masaCatalogButton.Refresh(null, null);
            }
            StateHasChanged();
        }

        var temp = NavigationManager.Uri.Contains("entries/index");
        if (temp != showEntryStyle)
        {
            showEntryStyle = temp;
            StateHasChanged();
        }
    }

    #region 释放实例

    public void Dispose()
    {
        NavigationManager.LocationChanged -= LocationChanged;
        GC.SuppressFinalize(this);
    }
    #endregion
}
